package com.hzdh.account.controller.engine;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.hzdh.account.entity.login.User;
import com.hzdh.account.utils.UriUtility;

@Controller
@RequestMapping("/hzdh")
public class TaskController {
	
	private static Logger logger = LoggerFactory.getLogger(TaskController.class);
	
//	@RequestMapping("/index")
//	public String indexView(HttpServletRequest request) {
//		User user = (User) request.getSession().getAttribute("userSession");
//		if (user != null) {
//			logger.info("", user);
//			request.getSession().setAttribute("userSession", user);
//		}
//		return "index";
//	}
	
	@RequestMapping("/{parentPath}/{path}/view")
	public String mainView(@PathVariable("parentPath") String parentPath , @PathVariable("path") String path, HttpServletRequest request) {
		
		User sessionUser = (User) request.getSession().getAttribute("userSession");
		String userId = sessionUser.getUserId();
		String userName = sessionUser.getUserName();
		String phone = sessionUser.getPhone();
		String realName = sessionUser.getRealName();
		String userAccount = sessionUser.getUserAccount();
		
		Map<String, String> queryParameters = new HashMap<>();
		queryParameters.put("userId", userId);
		queryParameters.put("phone", phone);
		queryParameters.put("userName", userName);
		queryParameters.put("realName", realName);
		queryParameters.put("userAccount", userAccount);
		
		System.out.println("into parameters is : " + queryParameters);
		
		StringBuffer urlPath = new StringBuffer("/"); 
		if (StringUtils.isBlank(path)) {
			urlPath = urlPath.append(parentPath);
		} else {
			urlPath = urlPath.append(parentPath).append("/").append(path);
		}
		request.setAttribute("sessionUser", sessionUser);
		String result =  "forward:" + UriUtility.getQueryString(urlPath.toString(), queryParameters);
		return result;
	}
}
